#include "GameLoopHandler.h"

namespace GeorgeLib
{

// Initialise all our static pointers to NULL first before we do anything
_GameLoopHandler*	_GameLoopHandler::GameLoopHandler_Instance	= NULL;
float				_GameLoopHandler::framerate					= 48;
float				_GameLoopHandler::updaterate				= 60;
double				_GameLoopHandler::FPSprevTime;
double				_GameLoopHandler::RTLprevTime;
	

_GameLoopHandler::_GameLoopHandler(void)
{
}

_GameLoopHandler::~_GameLoopHandler(void)
{
	// Delete the Instance
	if(GameLoopHandler_Instance != NULL)
	{
		delete GameLoopHandler_Instance;
		GameLoopHandler_Instance = NULL;
	}
}

_GameLoopHandler* _GameLoopHandler::getInstance(void)
{
	if(GameLoopHandler_Instance == NULL)
		GameLoopHandler_Instance = new _GameLoopHandler();

	return GameLoopHandler_Instance;
}

float _GameLoopHandler::getFramerate(void)
{
	return framerate;
}

float _GameLoopHandler::getUpdaterate(void)
{
	return updaterate;
}

void _GameLoopHandler::setFramerate(float a)
{
	framerate = a;
}

void _GameLoopHandler::setUpdaterate(float a)
{
	updaterate = a;
}

bool _GameLoopHandler::LimitRendering(void)
{
	return( Timer->GetDelayPrecise( 1000.0 / framerate, FPSprevTime ) );
}

bool _GameLoopHandler::LimitUpdating(void)
{
	return( Timer->GetDelayPrecise( 1000.0 / updaterate, RTLprevTime, true ) );
}

void _GameLoopHandler::LimitRendering(void (*callback)(void))
{
	if( Timer->GetDelayPrecise( 1000.0 / framerate, FPSprevTime, true ) )
		callback();
}

void _GameLoopHandler::LimitUpdating(void (*callback)(void))
{
	if( Timer->GetDelayPrecise( 1000.0 / updaterate, RTLprevTime, true ) )
		callback();
}

} // namespace GeorgeLib